/*
 * main.cpp
 *
 *  Created on: 2011-4-27
 *      Author: chenl
 */

#include "config.h"
#include "mainapp.h"
#include <signal.h>

static void reg_signal();

void sigusr1_handler(int);
void sigusr2_handler(int);

int main(int argc, char **argv)
{
	int32_t ret = S_OK;
	reg_signal();

	if (0 > ret)
	{
		return ret;
	}

	//初始化主程序
	CMainApp& mainApp = CREATE_MAINAPP_INSTANCE();
	ret = mainApp.Initialize();
	if (0 > ret)
	{
		return ret;
	}
	//處理命令參數
	ret = mainApp.ProcessCommand(argc, argv);
	if (0 > ret)
	{
		return ret;
	}
	//啓動各個綫程
	mainApp.Start();
	//主程序開始運行
	mainApp.Run();
	//主程序結束
	ret = mainApp.Uninitialize();
	if (0 > ret)
	{
		return ret;
	}
	return S_OK;
}

void reg_signal()
{
	signal(SIGUSR1, sigusr1_handler);
	signal(SIGUSR2, sigusr2_handler);
}

void sigusr1_handler(int)
{
	CMainApp& mainApp = GET_MAINAPP_INSTANCE();
	mainApp.Reload();
	signal(SIGUSR1, sigusr1_handler);
}
void sigusr2_handler(int)
{
	CMainApp& mainApp = GET_MAINAPP_INSTANCE();
	mainApp.Terminate();
	signal(SIGUSR1, sigusr1_handler);
}
